package com.douban.business;

import java.awt.PageAttributes.MediaType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;

import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParserConfiguration;
import org.cyberneko.html.HTMLConfiguration;

import com.core.ConnectionFactory;
import com.core.ContentFetcher;
import com.core.DoubanProperties;
import com.core.DoubanURL;
import com.core.UserRequest;
import com.douban.business.vo.ItemVO;
import com.douban.constants.DoubanMediaTypeConstant;
import com.douban.constants.DoubanUserActionConstant;


/**
 * The class that can load content for only one url
 * @author SELuoc
 *
 */
public class DoubanContentListFetcher extends ContentFetcher {


//	String mediatype;
//	String useraction;
	int start;
	private String username;
	private UserRequest ur; 
	
	public DoubanContentListFetcher(String username, DoubanMediaTypeConstant mediatype, DoubanUserActionConstant useraction, int start, int size){
		super(DoubanURL.getInstance().getURL(username,mediatype.getValue(), useraction.getValue(), start));
		//create a single request
		ur = new DoubanUserRequest(username,new DoubanMediaTypeConstant[]{mediatype}, new DoubanUserActionConstant[]{useraction});
		this.username = username;
//		this.mediatype = mediatype.getValue();
//		this.useraction = useraction.getValue();
		this.start = start;
	}

	public int loadContent() throws IOException{
		
		DoubanItemListHandler handler = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(handler);
		URLConnection connection = ConnectionFactory.createURLConnection(url) ;
		connection.connect();
		String encoding = connection.getContentEncoding();
        Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream())); 
		XMLInputSource src = new XMLInputSource(null, null, null, reader, encoding);	  
        //put the map into a centralized content container
		xmlReaderParser.parse(src);
		reader.close();		
	
		printOutItemsMap(handler.getMap());
		
		return handler.getMap().size();
		
	}
	
	private void printOutItemsMap(Map theMap){
		Iterator keysItr= theMap.keySet().iterator();
		while (keysItr.hasNext()) {
			String key = (String) keysItr.next();
			ItemVO itemvo = (ItemVO)theMap.get(key);
			System.out.println(itemvo.getId()+" "+itemvo.getTitle()+" "+itemvo.getIntro()+" "+itemvo.getDate());
			
		}
	}

	public UserRequest getUserRequest() {
		return ur;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int s) {
		this.start = s;
	}


	public int calcStart(int numberOfLoadedItems) {
		this.start = this.start + start;
		return this.start;		
	}
	
}
